<?php
class newsActions extends BaseAdminActions
{
	public $news;    public function __construct(){        parent::__construct();        
		Files::factory('News');
		$this->news = new News();
	}
	public function actionsIndex()
	{
		$this->addJs(MVC_WWW_HOST.'js/My97DatePicker/WdatePicker.js');
		$rs = $this->news->getNewsListBySearch();	
		$this->assign('rs', $rs);	
		Files::factory('System');
		$system = new System();
		$this->assign('columns', $system->getColumnsList());
	}
	public function actionsAdd()
	{
		if(Http::get('do')=='add'){
			$columns = 	Http::post('columns');
			$title = Http::post('title');	
			
			$source =  Http::post('source');	
			$author =  Http::post('author');
			$createby =  Http::post('createby');
			$tags =  Http::post('tags');			$swf =  Http::post('swf');
			$description =  strip_tags(Http::post('description'));
			$thumb =  Http::post('thumb');
			$contents =  Http::post('content');
			$ralation =  Http::post('ralation');
			$allowcomment =  Http::post('allowcomment');
			if ($allowcomment=='true') {
				$allowcomment=1;
			}else{
				$allowcomemnt = 0;
			}
			$data = array(
			'columns'=>$columns,
			'title'=>$title,
			
			'author'=>$author,
			'source'=>$source,
			'status'=>1,
			'createby'=>$createby,
			'createTime'=>time(),
			'updateTime'=>time(),
			'updateby'=>$createby,
			'tags'=>$tags,			'swf'=>$swf,
			'description'=>$description,
			'thumb'=>$thumb,
			'content'=>$contents,
			'ralation'=>$ralation,
			'allowcomment'=>$allowcomment
			);
			$newsId = $this->news->add($data);
			echo $newsId;
			die();
		}
		$this->assign('news', '');
		Files::factory('System');
		$system = new System();
		$this->assign('columns', $system->getColumnsList());		 
	}
	public function actionsModify()
	{
		$newsId = Http::get('id');
		if (Http::get('do')=='update'){
			$title = Http::post('title');
			$columns = 	Http::post('columns');
			
			$source =  Http::post('source');		
			$author =  Http::post('author');
			$createby =  Http::post('createby');
			$tags =  Http::post('tags');			$swf =  Http::post('swf');
			$description =  strip_tags(Http::post('description'));
			$thumb =  Http::post('thumb');
			$contents =  Http::post('content');
			$ralation =  Http::post('ralation');
			$allowcomment =  Http::post('allowcomment');
			if ($allowcomment=='true') {
				$allowcomment=1;
			}else{				$allowcomemnt = 0;
			}
			$data = array(
			'columns'=>$columns,
			'title'=>$title,			
			'author'=>$author,
			'source'=>$source,
			'status'=>1,
			'updateTime'=>time(),
			'updateby'=>$createby,
			'tags'=>$tags,			'swf'=>$swf,
			'description'=>$description,
			'thumb'=>$thumb,
			'content'=>$contents,
			'ralation'=>$ralation,			'allowcomment'=>$allowcomment
			);
			$this->news->update($newsId,$data);
			echo $newsId;
			die();
		}else{
			$news = $this->news->getNews($newsId);	
			$tagsArr = $this->news->getTags($newsId);
			$tags='';
			foreach ($tagsArr as $k=>$v){
				$tags.= $v['name'].';';
			}			$news['tags']=$tags;
			$relationArr = $this->news->getRelations($newsId);
			$relations='';			foreach ($relationArr as $k=>$v){
				$relations.= ';'.$v['rid'].';';
			}
			$news['ralation']=$relations;	
			$this->assign('news', $news);
			Files::factory('System');
			$system = new System();
			$this->assign('columns', $system->getColumnsList());
			$this->display('add.tpl.php'); 
		}
	}
	public function actionsDel()
	{		
		if($this->news->del(Http::get('id')))
		{
			echo '删除成功！';
		}else{
			echo '删除失败！';
		}
		die();
	}
	public function actionsCate()
	{		
		$cate = $this->news->getCateList();
		$this->assign('cate', $cate);
	}
	public function actionsCatedel()
	{		
		if($this->news->delCate(Http::get('id')))
		{
			echo '删除成功！';
		}else{
			echo '删除失败！';
		}
		die();
	}
	public function actionsCateadd()
	{
		if (Http::get('do')=='add'){
			$name=Http::get('name');
			$sort=Http::get('sort');
			if (empty($name)){
				die(json_encode(array('status'=>0,'html'=>'名称不能为空')));
			}else {
				
				if($this->news->addCate($name,$sort)){
					die(json_encode(array('status'=>1,'html'=>'添加成功')));
				}else{
					die(json_encode(array('status'=>0,'html'=>'添加失败')));					
				}
			}
			
		}
		$this->assign('cate', '');
	}
	
	
	public function actionsCatemodify()
	{
		$cateId = Http::get('id');
		if (Http::get('do')=='update'){
			$name=Http::get('name');
			$sort=Http::get('sort');
			if (empty($name)){
				die(json_encode(array('status'=>0,'html'=>'名称不能为空')));
			}else {
				
				if($this->news->updateCate($cateId,$name,$sort)){
					die(json_encode(array('status'=>1,'html'=>'修改成功')));
				}else{
					die(json_encode(array('status'=>0,'html'=>'修改失败')));					
				}
			}
		}
		
		
		$cate = $this->news->getCate($cateId);
		$this->assign('cate', $cate);
		$this->display('cateadd.tpl.php');	
	}
	
	public function actionsTocate()
	{
		$id = Http::get('id');
		$cateid = Http::get('cateid');
		
		if ($this->news->toCate($id,$cateid)){
			echo '转移成功！';
		}else{
			echo '转移失败！';
		}
		die();
	}
	
	public function actionsTocolumns(){
		$id = Http::get('id');
		$toColumns = Http::get('columnsid');
		
		if ($this->news->toColumns($id,$toColumns)){
			echo '转移成功！';
		}else{
			echo '转移失败！';
		}
		die();
	}
	
	public function actionsMatchs()	{	    $rs = $this->video->getAllMatchs();	    $this->assign('rs', $rs);	}	public function actionsMatchsset()	{	    $do = Http::get('do');	    if ($do){	        $res = '失败';	        if ($do=='del'){	            $sql ="DELETE FROM ".$this->video->t_matchs." WHERE id= '".Http::get('id')."' LIMIT 1 ";	            $this->video->db->delete($sql);	            die('删除成功');	        }	        $data = array(	                'name'=>Http::post('name'),	                'type'=>Http::post('type'),	                'info'=>Http::post('info'),	                'logo1'=>Http::post('logo1'),	                'logo2'=>Http::post('logo2'),	                'space'=>Http::post('space'),	                'link'=>Http::post('link'),	                'stime'=>Http::post('stime'),	                'etime'=>Http::post('etime')	        );	        if ($do=='add'){	            if($this->video->db->insert($this->video->t_matchs, $data)){	                $res = '添加成功';	            }	        }	        if ($do=='update'){	            if($this->video->db->updateData($this->video->t_matchs, $data, " WHERE id = ".Http::get('id'))){	                $res = '修改成功';	            }	        }		        die($res);	    }	    if (Http::get('id')){	        $row = $this->video->getOneMatchs(Http::get('id'));	    }else {	        $row = false;	    }	    $this->assign('row', $row);	}
	
	
	
	
}